package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.entity.Book;
import com.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

//@Controller
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    //查所有
    @RequestMapping(value ="/book",method = RequestMethod.GET)
    public Object getList(){
        List<Book> bookList = bookService.findAll();
        return  bookList;
    }
    //查一个
    @RequestMapping(value ="/book/{id}",method = RequestMethod.GET)
    public Object getBook(@PathVariable("id") Integer id){
        Book book = bookService.findById(id);
        return  book;
    }
    //分页查询
    //查一个
    @RequestMapping(value ="/book/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Object pageFind(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        PageInfo<Book> pageInfo = bookService.findPaged(pageNum, pageSize);
        return  pageInfo;
    }

    //保存
    @RequestMapping(value = "/book",method = RequestMethod.POST)
    public String  save(@RequestBody Book book){
        bookService.save(book);
//        System.out.println(book);
        return "success";
    }
    @RequestMapping(value = "/book",method = RequestMethod.PUT)
    public String  toUpdate(@RequestBody Book book){
        bookService.update(book);
        return "success";
    }
    //删除一个
    @RequestMapping(value ="/book/{id}",method = RequestMethod.DELETE)
    public String delBook(@PathVariable("id") Integer id){
        bookService.deleteById(id);
        return  "success";
    }

   @DeleteMapping("/book")
    public String delMulBook(@RequestBody Map<String,List<Integer>> ids){
        System.out.println(ids+"删除多个");
        bookService.batchDelete(ids.get("ids"));
        return  "success";
    }


}
